﻿using System;
using System.Data;
using System.Configuration;
using mojoPortal.Data;

namespace ud_Poll.Data
{
    
    public static class DBPollOption
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {
            if (ConfigurationManager.AppSettings["MSSQLWriteConnectionString"] != null)
            {
                return ConfigurationManager.AppSettings["MSSQLWriteConnectionString"];
            }

            return ConfigurationManager.AppSettings["MSSQLConnectionString"];

        }

        public static String dbPlatform()
        {
            return "MSSQL";
        }

        public static IDataReader GetPollOptions(Guid pollGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_PollOptions_Select", 1);
            sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
            return sph.ExecuteReader();

        }


        public static IDataReader GetPollOption(Guid optionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "mp_PollOptions_SelectOne", 1);
            sph.DefineSqlParameter("@OptionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, optionGuid);
            return sph.ExecuteReader();

        }

        public static bool IncrementVotes(
            Guid pollGuid,
            Guid optionGuid,
            Guid userGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_PollOptions_IncrementVotes", 3);
            
            sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
            sph.DefineSqlParameter("@OptionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, optionGuid);
            sph.DefineSqlParameter("@UserGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, userGuid);
            
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }


        public static int Add(
            Guid optionGuid,
            Guid pollGuid,
            string answer,
            int order)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_PollOptions_Insert", 4);
            sph.DefineSqlParameter("@OptionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, optionGuid);
            sph.DefineSqlParameter("@PollGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, pollGuid);
            sph.DefineSqlParameter("@Answer", SqlDbType.NVarChar, 255, ParameterDirection.Input, answer);
            sph.DefineSqlParameter("@Order", SqlDbType.Int, ParameterDirection.Input, order);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        public static bool Update(
            Guid optionGuid,
            string answer,
            int order)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_PollOptions_Update", 3);
            sph.DefineSqlParameter("@OptionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, optionGuid);
            sph.DefineSqlParameter("@Answer", SqlDbType.NVarChar, 255, ParameterDirection.Input, answer);
            sph.DefineSqlParameter("@Order", SqlDbType.Int, ParameterDirection.Input, order);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        public static bool Delete(Guid optionGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_PollOptions_Delete", 1);
            sph.DefineSqlParameter("@OptionGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, optionGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }



    }
}
